package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.commonutils.ExceptionUtils;
import com.atguigu.commonutils.ResultCodeEnum;
import com.atguigu.commonutils.result.R;
import com.atguigu.guli.service.base.Exception.GuliException;
import com.atguigu.guli.service.edu.entity.Subject;
import com.atguigu.guli.service.edu.entity.vo.SubjectVo;
import com.atguigu.guli.service.edu.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.runtime.parser.node.PutExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author wangyang
 * @since 2021-05-24
 */
@Api(tags = "课程管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("admin/edu/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @ApiOperation("课程Excel批量上传")
    @PostMapping("/import")
    public R importSubject(
            @ApiParam(value = "上传的文件",required = true)
            @RequestBody MultipartFile file
    ) {
        try{
            InputStream inputStream = file.getInputStream();
            subjectService.importSubject(inputStream);
            return R.ok().message("批量导入成功");
        }catch (Exception e) {
            log.info(ExceptionUtils.getMessage(e));
            throw new GuliException(ResultCodeEnum.EXCEL_DATA_IMPORT_ERROR);
        }

    }

    @ApiOperation("嵌套查询课程列表")
    @GetMapping("/nested-list")
    public R nestesList() {
       List<SubjectVo> subjectVos = subjectService.nestesList();
       return R.ok().data("items",subjectVos);
    }
}

